package com.clh.seckill.service.impl;

import ch.qos.logback.core.util.TimeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clh.seckill.exception.GlobalException;
import com.clh.seckill.mapper.UserMapper;
import com.clh.seckill.pojo.User;
import com.clh.seckill.service.UserService;
import com.clh.seckill.utils.CookieUtil;
import com.clh.seckill.utils.MD5Util;
import com.clh.seckill.utils.UUIDUtil;
import com.clh.seckill.utils.VaildatorUtil;
import com.clh.seckill.vo.LoginVo;
import com.clh.seckill.vo.RespBean;
import com.clh.seckill.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cailinhu
 * @since 2021-10-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
//    @Autowired
//   private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public RespBean doLogin(LoginVo vo, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(vo.getMobile())||StringUtils.isEmpty(vo.getPassword()))
        {
            throw  new GlobalException(RespBeanEnum.LOGIN_NULL);
        }
        User user = this.getById(vo.getMobile());
        if (!ObjectUtils.isEmpty(user)){
            String pwd = MD5Util.formPassToBoPass(vo.getPassword(), user.getSlat());
            if(pwd.equals(user.getPassword())){
                //生成cookie
                String uuid = UUIDUtil.uuid();
//                request.getSession().setAttribute(uuid,user);\
                //设置失效时间为20秒
                redisTemplate.opsForValue().set("user:"+uuid,user,60*60, TimeUnit.SECONDS);
                CookieUtil.addCookie(response,"userTick",uuid,0);
                return RespBean.success(uuid);
            }
        }
        throw  new GlobalException(RespBeanEnum.LOGIN_ERROR);
    }

    @Override
    public RespBean register(LoginVo vo) {
        //密码加密
        String pass = MD5Util.formPassToBoPass(vo.getPassword(), MD5Util.SALT);
        User user = new User(Long.valueOf(vo.getMobile()), "admin", pass, MD5Util.SALT, null, new Date(), new Date(), 0);
        super.save(user);
        return RespBean.error(RespBeanEnum.LOGIN_NULL,vo);
        //加密操作
    }

    @Override
    public User getUserByCookie(String userTicket,HttpServletResponse response) {
        if (!StringUtils.isEmpty(userTicket)){
            User user= (User)redisTemplate.opsForValue().get("user:"+userTicket);
            //再更新下cookie
            if (!ObjectUtils.isEmpty(user)){

                CookieUtil.addCookie(response,"userTick",userTicket,0);
            }
            return user;
        }
        return null;
    }
}
